package com.pxmeta.app.controller.third;

import com.google.common.collect.Maps;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.agora.RtmTokenBuilder;
import com.pxmeta.party.entity.vo.KickingVO;
import com.pxmeta.party.service.AgoraService;
import com.pxmeta.user.entity.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;

import static com.pxmeta.common.utils.agora.RtmTokenBuilder.Role.Rtm_User;

@RestController
@RequestMapping("/agora")
@Api(tags = "声网", value = "声网")
@RequiredArgsConstructor
public class AgoraController {


    @Value("${agora.appId}")
    private String appId;

    @Value("${agora.certificate}")
    private String certificate;

    private static final int expirationTimeInSeconds = 3600 * 72;

    private final AgoraService agoraService;


    @GetMapping("/getMediaToken")
    @ApiOperation("获取语音通话token")
    public AjaxResult getToken(@RequestParam String uid) {
        RtmTokenBuilder token = new RtmTokenBuilder();
        HashMap<String, Object> map = Maps.newHashMap();
        try{
            String result = token.buildToken(appId, certificate, uid, Rtm_User, expirationTimeInSeconds);
            map.put("rtmToken",result);
            return AjaxResult.success(map);
        }catch (Exception e){
            return AjaxResult.error();
        }
    }


    @ApiOperation("创建规则")
    @PostMapping("/kickingRule")
    public AjaxResult kickingRule(KickingVO vo) {
        agoraService.kickingRule(vo);
        return AjaxResult.success();
    }


    @GetMapping("/getUsers")
    public AjaxResult getUsers(@RequestParam String channelName) {
        List<UserDTO> users = agoraService.getUsers(appId, channelName);
        return AjaxResult.success(users);
    }


//    @GetMapping("/exportMusic")
//    public AjaxResult exportMusic() {
//        Long pageCode = null;
//        for (int i = 0; i < 2000; i++) {
//            HashMap<String, String> map = Maps.newHashMap();
//            map.put("size", "1000");
//            if (pageCode != null) {
//                map.put("pageCode", pageCode.toString());
//            }
//
//            JSONObject post = agoraClient.doGet("/ktv-service/api/serv/songs", map);
//            String data = post.getString("list");
//            List<MusicVO> list = JSONObject.parseArray(data, MusicVO.class);
//            List<Music> musicList = list.stream().map(l -> {
//                Music music = new Music();
//                music.setId(l.getSongCode());
//                music.setDuration(l.getDuration());
//                music.setStatus(l.getStatus());
//                if (CollectionUtil.isNotEmpty(l.getLyricType())) {
//                    music.setLyricType(StrUtil.join(",", l.getLyricType()));
//                }
//                music.setName(l.getName());
//                music.setPoster(l.getPoster());
//                music.setSinger(l.getSinger());
//                music.setType(l.getType());
//                music.setPitchType(l.getPitchType());
//                return music;
//            }).collect(Collectors.toList());
//            if (list.size() == 1000) {
//                pageCode = list.get(999).getSongCode();
//            } else {
//                if (CollectionUtil.isNotEmpty(musicList)) {
//                    musicService.saveBatch(musicList);
//                }
//                break;
//            }
//            musicService.saveBatch(musicList);
//        }
//        return AjaxResult.success();
//
//    }


    @GetMapping("/getHotMusic")
    @ApiOperation(value = "获取热门歌曲")
    public AjaxResult getHotMusic() {
        // 客户 ID
        final String customerKey = "Your customer key";
        // 客户密钥
        final String customerSecret = "Your customer secret";

        // 拼接客户 ID 和客户密钥并使用 base64 编码
        String plainCredentials = customerKey + ":" + customerSecret;
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;
        return AjaxResult.success();
    }


}
